package com.emip.servlet.authority;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emip.bean.AlarmConfigBean;
import com.emip.bean.RoleBean;
import com.emip.bean.SiteBean;
import com.emip.bean.UserBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.AlarmConfigService;
import com.emip.service.RoleService;
import com.emip.service.UserService;
import com.emip.util.TableFormat;

/**
 * 
 * @author Will
 *  用户管理
 */

@Controller
@RequestMapping(value="/authority/users")
public class UsersAction {

	@Autowired
	UserService userService;
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	AlarmConfigService alarmConfigService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/authorityFunc/UsersManagement";
	}
	
	@RequestMapping(value="/getAllUsers")
	public @ResponseBody Table<UserBean> getAllUsers() {
		List<UserBean> result = userService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<UserBean>().formTable(totalPages, 1, records, result);
	}
	
	@RequestMapping(value="/getUserByID")
	public @ResponseBody UserBean getUserByID(
			@RequestParam("eid") String eid) {
		return userService.getUserByID(eid);
	}
	
	@RequestMapping(value="/addUser")
	public @ResponseBody UserBean addUser(
			@RequestParam("eID") String id, 
			@RequestParam("eName") String name, 
			@RequestParam("ePassword") String password,
			@RequestParam("eMobilePhone") String mobilePhone, 
			@RequestParam("eMailAddress") String mailAddress, 
			@RequestParam("eRoleID") int roleID, 
			@RequestParam("eFlag") String flag, 
			@RequestParam("eControllFlag") String controllFlag,
			@RequestParam("eAlarmID") int alarmID,
			@RequestParam("elongitude") String slongitude,
			@RequestParam("elatitude") String slatitude,
			@RequestParam("eMapZoom") String sMapZoom) {
		RoleBean roleBean = roleService.getRoleByID(roleID);
		//若经纬度未填，则获取默认值
		double longitude, latitude;
		if ((slongitude=="") || (slatitude=="")) {
			longitude = SysParams.mapLongitude;
			latitude = SysParams.mapLatitude;
		} else {
			longitude = Double.valueOf(slongitude);
			latitude = Double.valueOf(slatitude);
		}
		int mapZoom;
		if (sMapZoom=="") {
			mapZoom = SysParams.mapZoom;
		} else {
			mapZoom = Integer.valueOf(sMapZoom);
		}
		AlarmConfigBean aBean = alarmConfigService.getAlarmConfigByID(alarmID);
		return userService.addUser(id, name, password, roleBean, mobilePhone, mailAddress, flag, "1", controllFlag, "", "", "", aBean, longitude, latitude, mapZoom);
	}
	
	@RequestMapping(value="/updateUser")
	public @ResponseBody UserBean updateUser(
			@RequestParam("eID") String id, 
			@RequestParam("eName") String name, 
			@RequestParam("ePassword") String password,
			@RequestParam("eMobilePhone") String mobilePhone, 
			@RequestParam("eMailAddress") String mailAddress, 
			@RequestParam("eRoleID") int roleID, 
			@RequestParam("eFlag") String flag,
			@RequestParam("eControllFlag") String controllFlag,
			@RequestParam("eAlarmID") int alarmID,
			@RequestParam("elongitude") String slongitude,
			@RequestParam("elatitude") String slatitude,
			@RequestParam("eMapZoom") String sMapZoom) {
		RoleBean roleBean = roleService.getRoleByID(roleID);
		//若经纬度未填，则获取默认值
		double longitude, latitude;
		if ((slongitude=="") || (slatitude=="")) {
			longitude = SysParams.mapLongitude;
			latitude = SysParams.mapLatitude;
		} else {
			longitude = Double.valueOf(slongitude);
			latitude = Double.valueOf(slatitude);
		}
		int mapZoom;
		if (sMapZoom=="") {
			mapZoom = SysParams.mapZoom;
		} else {
			mapZoom = Integer.valueOf(sMapZoom);
		}
		AlarmConfigBean aBean = alarmConfigService.getAlarmConfigByID(alarmID);
		return userService.updateUser(id, name, password, roleBean, mobilePhone, mailAddress, flag, "1", controllFlag, "", "", "", aBean, longitude, latitude, mapZoom);
	}
	
	@RequestMapping(value="/updateUserSimple")
	public @ResponseBody UserBean updateUserSimple(
			@RequestParam("eID") String id, 
			@RequestParam("eName") String name, 
			@RequestParam("eMobilePhone") String mobilePhone, 
			@RequestParam("eMailAddress") String mailAddress, 
			@RequestParam("elongitude") String slongitude,
			@RequestParam("elatitude") String slatitude,
			@RequestParam("eMapZoom") String sMapZoom,
			 HttpSession session) {
		//若经纬度未填，则获取默认值
		double longitude, latitude;
		if ((slongitude=="") || (slatitude=="")) {
			longitude = SysParams.mapLongitude;
			latitude = SysParams.mapLatitude;
		} else {
			longitude = Double.valueOf(slongitude);
			latitude = Double.valueOf(slatitude);
		}
		int mapZoom;
		if (sMapZoom=="") {
			mapZoom = SysParams.mapZoom;
		} else {
			mapZoom = Integer.valueOf(sMapZoom);
		}
		
		UserBean uBean = userService.updateUserSimple(id, name, mobilePhone, mailAddress, longitude, latitude, mapZoom);
		session.setAttribute("user", uBean);
		return uBean;
	}
	
	@RequestMapping(value="/updateUserPwd")
	public @ResponseBody String updateUserPwd(@RequestParam("oriPwd") String oriPwd,
			@RequestParam("newPwd1") String newPwd1, @RequestParam("newPwd2") String newPwd2, 
			HttpSession session) {
		//判断原密码是否正确
		UserBean user = (UserBean)session.getAttribute("user");
		if (userService.isUserPwd(user.getId(), oriPwd)==false) {
			return "oriError";
		}
		user = userService.updateUserPwd(user.getId(), newPwd1);
		if (user!=null) {
			return "pwdChanged";
		}
		return "failed";
	}
	
	
	@RequestMapping(value="/deleteUser")
	public @ResponseBody String deleteUser(
			@RequestParam("eID") String id) {
		userService.deleteUser(id);
		return "success"; 
	}
	
	
	@RequestMapping(value="/getAllSitesByUser")
	public @ResponseBody List<SiteBean> getAllSitesByUser(@RequestParam("userid") String userid) {
		return (List<SiteBean>)userService.getUserSites(userid);
		
	}
	
	@RequestMapping(value="/setUserSites")
	public @ResponseBody String setUserSites(@RequestParam("userID") String userID, 
			@RequestParam("siteIDs") String siteIDs) {
		siteIDs = siteIDs.substring(0, siteIDs.length()-1);
		String[] sIDs = siteIDs.split(",");
		userService.setUserSites(userID, sIDs);
		return "success";
	}
}
